<template>
    <el-select v-model="selectData" style="width:100%"
                filterable
                :clearable="clearable"
                @change='tieEnumSelectChange'
                :disabled="selectDisabled">
                <el-option
                    v-for="item in enumValueList"
                    :value="item.code"
                    :key="item.code"
                    :label="item.name">
                </el-option>
    </el-select>
</template>
<script>
import HttpHandler from "@/handler/HttpHandler";
export default {
    name: 'tie-enum',
    props:{
        status : {type : String },
        name:{ type : String, },
        sysName:{ type : String, default : process.env.VUE_APP_NAME},
        className:{ type : String, },
        default : {type : String },
        value : {type : String},
        clearable : {type : Boolean,default:false},
        disabled : {type:Boolean,default:false}
    },
    data(){
        return{
            tieHttp : new HttpHandler(),
            url : '/tie-system/resource/getEnum',
            selectData:'',
            enumValueList:[],
            allList : [],
            selectDisabled:false
        }
    },
    watch: {
        disabled(val){
            this.selectDisabled = val
        }
    },
    async created(){
        if(this.disabled) this.selectDisabled = this.disabled
        else this.selectDisabled = this.status==='view'?true:false
        let param = {sysName:this.sysName,className:this.className}
        await this.tieHttp.get(this.url,param).then(res=>{
            let enumValueList = res.data
            this.enumValueList = enumValueList
            this.allList = this.enumValueList
        })
        if(this.status == 'add'){
            if(this.default!=null)
                for(let i = 0 ; i < this.enumValueList.length ; i++ ){
                    if(this.enumValueList[i].name == this.default){
                        this.selectData = this.enumValueList[i].code
                        this.$emit('input',this.selectData)
                        this.$emit('tieEnumSelectChange',this.name,this.selectData)
                    }
                }
        }else{
            if(this.value!=null){
                for(let i = 0 ; i < this.enumValueList.length ; i++ ){
                    if(this.enumValueList[i].name == this.value){
                        this.selectData = this.enumValueList[i].code
                        this.$emit('input',this.selectData)
                        this.$emit('tieEnumSelectChange',this.name,this.selectData)
                    }
                }

            }
        }
    },
    methods:{
        isDisabled(flag){
            this.selectDisabled = flag
        },
        filterIn(codeList){
            let afterFilterList = [];
            for(let i = 0 ; i < this.allList.length ; i++ ){
                if(codeList.indexOf(this.allList[i].code) != -1){
                    afterFilterList.push(this.allList[i])
                }
            }
            this.enumValueList = afterFilterList
        },
        changeVal(val){
            this.selectData = val
        },
        tieEnumSelectChange(value){
            this.$emit('input',value)
            this.$emit('tieEnumSelectChange',this.name,value)
        }
    }
}
</script>
